var sign = require('./sign.js');
var request = require('request');
var fs = require('fs');
var path = require('path');
var moment = require('moment');
exports.getOrderList = function(req,res){
	sign.dataReq(req,req.session.token,sign.pageData(req.body),'order/getOrderList','post',function(ResData){
		if(ResData!=''){
	            console.log(ResData);
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
	// res.send({message: "您没有权限！", statusCode: 300});
}

exports.getChildOrderList = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'childOrder/getChildOrderList','post',function(ResData){
		if(ResData!=''){
			var ResData = {rows:ResData};
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
}
exports.updateChildOrderList = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'childOrder/updateChildOrderList','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
}
exports.addChildOrderList = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'childOrder/addChildOrderList','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
}
exports.delChildOrder = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'childOrder/delChildOrderList','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
}

exports.getOrderoldList = function(req,res){
   var  nameArr=[] ;
	sign.dataReq(req,req.session.token,sign.pageData(req.body),'order/getOrderList','post',function(ResData){
		if(ResData!=''){
			if(ResData.statusCode !== 300){
				ResData.rows.forEach(function myFunction(item,index){
					nameArr.push(item);
					ResData.rows = nameArr;
				});
			}
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
}
exports.getloadinglist = function(req,res){
   var  loadingArr=[] ;
	sign.dataReq(req,req.session.token,sign.pageData(req.body),'order/getOrderList','post',function(ResData){
		if(ResData!=''){
			if(ResData.statusCode !== 300){
				ResData.rows.forEach(function myFunction(item,index){
					loadingArr.push(item);
					ResData.rows = loadingArr;
				});
			}
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
}
exports.getNotBeginList = function(req,res){
   var  notBginArr=[] ;
	sign.dataReq(req,req.session.token,sign.pageData(req.body),'order/getOrderList','post',function(ResData){
		console.log(ResData);
		if(ResData!=''){
			if(ResData.statusCode !== 300){
			  ResData.rows.forEach(function myFunction(item,index){
			    notBginArr.push(item);
			    ResData.rows = notBginArr;
			   });
			}
			res.send(ResData);
		}else{
			console.log("没数据");
			res.send('暂时没有数据');
		}
	})
}

exports.addOrder = function(req,res){
      console.log(req.body);
	sign.importfileReq(req,req.session.token,req,'order/addOrder','post',function(ResData){
	    console.log("223")
	    console.log("ResData")
		if(ResData!=''){
			res.send(ResData);
		}
	})
	// console.log(req.body);	
	// sign.dataReq(req,req.session.token,req.body,'order/addOrder','post',function(ResDa~ta){
	// 	if(ResData!=''){
	// 		res.send(ResData);
	// 	}
	// })
}

exports.processStatistics = function(req,res){
	sign.dataReq(req,req.session.token,sign.pageData(req.body),'order/processStatistics','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}
	})
}

exports.alldevice = function(req,res){
	sign.dataReq(req,req.session.token,sign.pageData(req.body),'order/alldevice','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}
	})
}

exports.getTypeList = function(req,res){
	sign.nullDataReq(req,req.session.token,'order/getTypeList','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
}
exports.getConfigList = function(req,res){
	sign.nullDataReq(req,req.session.token,'order/getConfigList','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}else{
			res.send('暂时没有数据');
		}
	})
}
exports.getControllerList = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'order/getControllerList','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}
	})
}
exports.checkSN = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'order/checkSN','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}
	})
}
exports.addProduct =function(req,res){
	sign.dataReq(req,req.session.token,req.body,'order/addProduct','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}
	})
}
exports.updateInfo = function(req,res){
	console.log(req);
	console.log(req.body);
	sign.importfileReq(req,req.session.token,req,'order/updateOrder','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}
	})
}
exports.operateOrder = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'order/operateOrder','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}
	})
}
// exports.assignController= function(req,res){
// 	sign.dataReq(req,req.session.token,req.body,'order/assignController','post',function(ResData){
// 		if(ResData!=''){
// 			res.send(ResData);
// 		}
// 	})
// }

exports.getOrderController = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'order/getOrderController','post',function(ResData){
		res.send(ResData);
	})
}
// exports.delOrderController = function(req,res){
// 	sign.dataReq(req,req.session.token,req.body,'order/delOrderController','post',function(ResData){
// 		if(ResData!=''){
// 			res.send(ResData);
// 		}
// 	})
// }

exports.todayFinishInfo = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'index/todayFinishInfo','post',function(ResData){
		res.send(ResData);
	})
}
exports.todayFinishNum = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'index/todayFinishNum','post',function(ResData){
		res.send(ResData);
	})
}
exports.orderSituation = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'index/orderSituation','post',function(ResData){
		res.send(ResData);
	})
}
exports.getNewOrderInfo = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'index/getNewOrderInfo','post',function(ResData){
		res.send(ResData);
	})
}
exports.statisticsOneMonth = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'index/statisticsOneMonth','post',function(ResData){
		res.send(ResData);
	})
}

exports.processList = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'order/processList','post',function(ResData){
	  if(ResData!=''){
		res.send(ResData);
	}
	})
}

exports.productList = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'order/productList','post',function(ResData){
		res.send(ResData);
	})
}

exports.orderProcessList = function(req,res){
	sign.dataReq(req,req.session.token,req.body,'order/orderProcessList','post',function(ResData){
	// console.log(ResData)
		res.send(ResData);
	})
}
exports.saveProcessList = function(req,res){
	data = req.body;
	var p =JSON.parse(data.processList);
	  data.processList = p;
	//   console.log(data);
	sign.dataReq(req,req.session.token,data,'order/saveProcessList','post',function(ResData){
		res.send(ResData);
	})
}

exports.getDeviceReportDate = function(req,res){
	data = req.body;
	sign.dataReq(req,req.session.token,sign.pageData(req.body),'order/getDeviceReportDate','post',function(ResData){
		if(ResData!=''){
			res.send(ResData);
		}
	})
}

exports.getMlti = function(req,res){  //订单 工序生产详情
	data = req.body;
	console.log(data);
	// console.log(data.values);
	// console.log(JSON.parse(data.values));
	if(data.key){ //存在key为条件搜索
		console.log(data.values)
		console.log("前")
		var values =  JSON.parse(data.values)
		data.values = values
		console.log("后")
		console.log(data.values)

	}
	console.log(sign.pageData(data));
	sign.dataReq(req,req.session.token,sign.pageData(data),'order/getDeviceReportDateByProcessId','post',function(ResData){
		if(ResData!=''){
			console.log(ResData);
			res.send(ResData);
		}
	})
}
  
exports.export = function(req,res){
	request.get('http://192.168.2.140:8080/order/export?orderId='+req.body.orderId)
	.on('end', function() 
	  {
	  	var file = __dirname + '/../public/order.xls'; //__dirname返回本文件的所在的文件夹
	  	file  = path.resolve(file); //path.resolve返回device.xls的绝对路径
	  	//方案一
	    // res.download(file);
	    //方案二
	    var filestream = fs.createReadStream(file);
	    res.attachment(moment.unix(new Date() / 1000).format('YYYY-MM-DD')+'.xls');  //设置下载文件名称
	    res.set('Content-Type','application/octet-stream');//传输类型
	    filestream.pipe(res);
	  })
  	.pipe(fs.createWriteStream('public/order.xls'));//通过管道把写入文件流  
}
